<?php include('../resources/views/header.blade.php'); ?>

<section>
    <div style="clear:both;padding:4px;">
        <p style="font-size:20px;padding-left:6px;padding-right:6px;"><?php if(isset($question)){ echo $question[0]->title; } ?></p>
    </div>
    <?php
        if( isset($answer) && $answer[0]->creator_id == $_SESSION['user_id'] ){
            echo '<div style="float:right;margin-right:12px;">';
            echo '<a href="/edit_answer/'.$answer[0]->id.'"><span style="color:#58bdfd;font-size:16px;">编辑</span></a>';
            echo '</div>';
            echo '<div style="clear:both;float:right;margin-right:12px;margin-top:12px;">';
            echo '<span style="color:#ff524d;font-size:16px;" id="delete_answer">删除</span>';
            echo '</div>';
        }
    ?>

    <div style="padding-left:12px;padding-right:12px;margin-bottom:16px;margin-top:24px;">
        <!-- <div style="margin-bottom:6px;"><span style="color:#888;">回答者</span></div> -->
        <img class="header" src="http://source.timepicker.cn/<?php if(isset($answer)){ echo $answer[0]->icon; } ?>" style="float:left;width:22px;height:22px;margin-left:4px;" />
        <div style="margin-left:34px;">
            <span style="line-height:24px;font-size:16px;"><?php if(isset($answer)){ echo $answer[0]->name; } ?></span>
        </div>
    </div>
</section>

<section style="width:100%;background-color:#fff;border-top:1px solid #eee;margin-bottom:-12px;">
    <div style="padding-left:16px;padding-right:16px;padding-bottom:10px;font-size:17px;line-height:28px;overflow-x:hidden;">
        <p style="">
            <?php if(isset($answer)){
                $target = str_replace("\r\n" , "<br />" , $answer[0]->content);
                echo $target;
            } ?>
        </p>

        <div style="float:right;">
            <span style="font-size:13px;color:#999;">
                回答于 <?php if(isset($answer)){ echo $answer[0]->add_time; } ?>
            </span>
        </div>
    </div>
</section>

<section style="padding-top:16px;padding-bottom:12px;border-top:1px solid #eee;margin-top:0px;position:fixed; bottom:0;width:100%;background-color:#F8F9FA;margin-bottom:0px;left:0;">
    <div style="width:100%;overflow:hidden;">
        <div style="width:25%;float:left;text-align:center;" id="up_btn">
            <?php
                if( isset($up) && $up == 1 ){
                    echo '<img src="../images/up_light.png" style="width:24px;height:24px;margin:0 auto;" id="up_img" />';
                    echo '<span style="display:block;font-size:12px;margin-top:4px;" id="up_text">已顶</span>';
                }else{
                    echo '<img src="../images/up.png" style="width:24px;height:24px;margin:0 auto;" id="up_img" />';
                    echo '<span style="display:block;font-size:12px;margin-top:4px;" id="up_text">顶</span>';
                }
            ?>
        </div>
        <div style="width:25%;float:left;text-align:center;" id="down_btn">
            <?php
                if( isset($down) && $down == 1 ){
                    echo '<img src="../images/down_light.png" style="width:24px;height:24px;margin:0 auto;" id="down_img" />';
                    echo '<span style="display:block;font-size:12px;margin-top:4px;" id="down_text">已踩</span>';
                }else{
                    echo '<img src="../images/down.png" style="width:24px;height:24px;margin:0 auto;" id="down_img" />';
                    echo '<span style="display:block;font-size:12px;margin-top:4px;" id="down_text">踩</span>';
                }
            ?>
        </div>
        <div style="width:25%;float:left;text-align:center;" id="report_btn">
            <?php
                if( isset($report) && $report == 1 ){
                    echo '<img src="../images/jubao_light.png" style="width:24px;height:24px;margin:0 auto;" id="report_img" />';
                    echo "<span style='display:block;font-size:12px;margin-top:4px;' id='report_text'>已举报</span>";
                }else{
                    echo '<img src="../images/jubao.png" style="width:24px;height:24px;margin:0 auto;" id="report_img" />';
                    echo "<span style='display:block;font-size:12px;margin-top:4px;' id='report_text'>举报</span>";
                }
            ?>
        </div>
        <div style="width:25%;float:left;text-align:center;" id ="answer_comment">
            <img src="../images/comment.png" style="width:24px;height:24px;margin:0 auto;" id="comment_img" />
            <span style="display:block;font-size:12px;margin-top:4px;" id="comment_span">评论</span>
        </div>
    </div>
</section>

<?php include('../resources/views/footer.blade.php'); ?>


<script type="text/javascript">
    var up      = <?php if( isset($up) ){ echo $up;}else{ echo 0; } ?>;
    var down    = <?php if( isset($down) ){ echo $down;}else{ echo 0; } ?>;
    var report  = <?php if( isset($report) ){ echo $report;}else{ echo 0; } ?>;

    var ua          = "<?php if(isset($user_agent)){ echo $user_agent;} ?>";

    var object_id   = <?php if(isset($answer_id)){ echo $answer_id;} ?>;

    var comment_count = "<?php if(isset($comment_count)){ echo $comment_count;}else{echo 0;} ?>";
    $("#comment_span").html("评论 ("+comment_count+")")

    $("#answer_comment").on('click' , function(){
        var payload = {
            type : 'Comment' ,
            content : {
                object : 'wenda' ,
                object_id : object_id ,
                app_id : 23 ,
                count : comment_count
            }
        };
        if( ua == 'ptimeios' ){
            window.webkit.messageHandlers.webViewApp.postMessage(payload);
        }else{
            window.AndroidJsBridge.postMessage( JSON.stringify(payload) );
        }
    });


    $("#report_btn").on('click' , function(){
        $.ajax({
            type : "POST" ,
            url : "/report/"+object_id ,
            data : {"value" : report} ,
            dataType : "json" ,
            success : function(data){
                if( data.error ){
                    toast(data.message);
                }else{
                    if( report == 1 ){
                        report = 0;
                        $("#report_img").attr('src' , '../images/jubao.png');
                        $("#report_text").html("举报");
                        toast('取消成功');
                    }else{
                        report = 1;
                        $("#report_img").attr('src' , '../images/jubao_light.png');
                        $("#report_text").html("已举报");
                        toast('举报成功');
                    }

                }
            }
        });
    });


    $("#up_btn").on('click' , function(){
        $.ajax({
            type : "POST" ,
            url : "/up/"+object_id ,
            data : {"value" : up} ,
            dataType : "json" ,
            success : function(data){
                if( data.error ){
                    toast(data.message);
                }else{
                    if( up == 1 ){
                        up = 0;
                        $("#up_img").attr('src' , '../images/up.png');
                        $("#up_text").html("顶");
                        toast('取消成功');
                    }else{
                        up = 1;
                        $("#up_img").attr('src' , '../images/up_light.png');
                        $("#up_text").html("已顶");
                        toast('成功顶了一下');
                    }
                }
            }
        });
    });


    $("#down_btn").on('click' , function(){
        $.ajax({
            type : "POST" ,
            url : "/down/"+object_id ,
            data : {"value" : down} ,
            dataType : "json" ,
            success : function(data){
                if( data.error ){
                    toast(data.message);
                }else{
                    if( down == 1 ){
                        down = 0;
                        $("#down_img").attr('src' , '../images/down.png');
                        $("#down_text").html("踩");
                        toast('取消成功');
                    }else{
                        down = 1;
                        $("#down_img").attr('src' , '../images/down_light.png');
                        $("#down_text").html("已踩");
                        toast('踩了一下');
                    }
                }
            }
        });
    });


    $("#delete_answer").on('click' , function(){

    });

</script>
